﻿using System.Web.Security;
using System.Collections.Generic;
using System.Configuration.Provider;
using System.Collections.Specialized;
using System;
using System.Data;
using System.Data.Odbc;
using System.Configuration;
using System.Diagnostics;
using System.Web;
using System.Globalization;
using System.Security.Cryptography;
using System.Text;
using System.Web.Configuration;
using System.Linq;
using Microsoft.SharePoint;

namespace ContosoProviders
{

    public sealed class Members : MembershipProvider
    {
        private LDAPMember _fbaLDAPProvider = null;
        private LDAPMember FBALDAPProvider
        {
            get
            {
                if (_fbaLDAPProvider == null)
                    _fbaLDAPProvider = new LDAPMember();
                return _fbaLDAPProvider;
            }
        }
        
        #region Override methods

        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            MembershipUserCollection users = new MembershipUserCollection();
            FBALDAPProvider.FindUsersByEmail(emailToMatch, pageIndex, pageSize, out totalRecords);
            
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    MemberShipProviderData memberShipProviderData = new MemberShipProviderData();
                    memberShipProviderData.FindUsersByEmail(emailToMatch.Trim().ToLower());
                });
            
            

            totalRecords = users.Count;

            return users;
        }

        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            MembershipUserCollection users = new MembershipUserCollection();
            FBALDAPProvider.FindUsersByName(usernameToMatch, pageIndex, pageSize, out totalRecords);
            
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {

                    MemberShipProviderData memberShipProviderData = new MemberShipProviderData();
                    memberShipProviderData.FindUsersByName(usernameToMatch.Trim().ToLower());
                });

            
            totalRecords = users.Count;

            return users;
        }

        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            FBALDAPProvider.GetAllUsers(pageIndex, pageSize, out totalRecords);
            MembershipUserCollection users = new MembershipUserCollection();
            
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    MemberShipProviderData memberShipProviderData = new MemberShipProviderData();
                    memberShipProviderData.GetAllUsers();
                });

            
            totalRecords = users.Count;

            return users;
        }

        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            FBALDAPProvider.GetUser(username, userIsOnline);
            MembershipUser mUser = null;
            
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    MemberShipProviderData memberShipProviderData = new MemberShipProviderData();
                    mUser = memberShipProviderData.GetUser(username.Trim().ToLower());
                });
            
            return mUser;
        }

        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            FBALDAPProvider.GetUser(providerUserKey, userIsOnline);
            MembershipUser mUser = null;
            
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    MemberShipProviderData memberShipProviderData = new MemberShipProviderData();
                    mUser = memberShipProviderData.GetUser(providerUserKey.ToString().Trim().ToLower());
                });
           
            

            return mUser;
        }

        public override string GetUserNameByEmail(string email)
        {
            FBALDAPProvider.GetUserNameByEmail(email);
            string userName = string.Empty;
            
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {

                    MemberShipProviderData memberShipProviderData = new MemberShipProviderData();
                    userName = memberShipProviderData.GetUserNameByEmail(email.Trim().ToLower());
                });
            
               
            

            return email;
        }

        public override bool ValidateUser(string username, string password)
        {
            bool isAuth = false;

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                MemberShipProviderData memberShipProviderData = new MemberShipProviderData();
                isAuth = memberShipProviderData.ValidateUser(username, password);
            });

            return isAuth;
        }

        #endregion

        #region Not override methods

        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            bool status = false;

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                MemberShipProviderData memberShipProviderData = new MemberShipProviderData();
                status = memberShipProviderData.ChangePassword(username, newPassword);
            });

            return status;
        }

        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            throw new NotImplementedException();
        }

        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            throw new NotImplementedException();
        }

        public override bool EnablePasswordReset
        {
            get { throw new NotImplementedException(); }
        }

        public override bool EnablePasswordRetrieval
        {
            get { throw new NotImplementedException(); }
        }

        public override int GetNumberOfUsersOnline()
        {
            throw new NotImplementedException();
        }

        public override string GetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }

        public override int MaxInvalidPasswordAttempts
        {
            get { throw new NotImplementedException(); }
        }

        public override int MinRequiredNonAlphanumericCharacters
        {
            get { throw new NotImplementedException(); }
        }

        public override int MinRequiredPasswordLength
        {
            get { throw new NotImplementedException(); }
        }

        public override int PasswordAttemptWindow
        {
            get { throw new NotImplementedException(); }
        }

        public override MembershipPasswordFormat PasswordFormat
        {
            get { throw new NotImplementedException(); }
        }

        public override string PasswordStrengthRegularExpression
        {
            get { throw new NotImplementedException(); }
        }

        public override bool RequiresQuestionAndAnswer
        {
            get { throw new NotImplementedException(); }
        }

        public override bool RequiresUniqueEmail
        {
            get { throw new NotImplementedException(); }
        }

        public override string ResetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }

        public override bool UnlockUser(string userName)
        {
            throw new NotImplementedException();
        }

        public override void UpdateUser(MembershipUser user)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}
